# -*- coding: utf-8 -*-

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or (at
#  your option) any later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

from qt import *
#from pseudovisa import *
from visa import *




class visatester(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        if not name:
            self.setName("visatester")


        visatesterLayout = QGridLayout(self,1,1,11,6,"visatesterLayout")

        self.buttonGroup2 = QButtonGroup(self,"buttonGroup2")
        self.buttonGroup2.setColumnLayout(0,Qt.Vertical)
        self.buttonGroup2.layout().setSpacing(6)
        self.buttonGroup2.layout().setMargin(11)
        buttonGroup2Layout = QGridLayout(self.buttonGroup2.layout())
        buttonGroup2Layout.setAlignment(Qt.AlignTop)

        layout18 = QVBoxLayout(None,0,6,"layout18")

        layout16 = QVBoxLayout(None,0,6,"layout16")

        layout13 = QHBoxLayout(None,0,6,"layout13")

        layout11 = QVBoxLayout(None,0,6,"layout11")

        self.textLabel1 = QLabel(self.buttonGroup2,"textLabel1")
        layout11.addWidget(self.textLabel1)

        self.textLabel2 = QLabel(self.buttonGroup2,"textLabel2")
        layout11.addWidget(self.textLabel2)
        layout13.addLayout(layout11)

        layout12 = QVBoxLayout(None,0,6,"layout12")

        self.gpibCombo = QComboBox(0,self.buttonGroup2,"gpibCombo")
        self.gpibCombo.setEditable(1)
	gpibs=get_instruments_list()
	self.gpibCombo.clear()
	for i in gpibs:
		self.gpibCombo.insertItem(i)	
        layout12.addWidget(self.gpibCombo)

        self.commandCombo = QComboBox(0,self.buttonGroup2,"commandCombo")
        self.commandCombo.setEditable(1)
        layout12.addWidget(self.commandCombo)
        layout13.addLayout(layout12)
        layout16.addLayout(layout13)

        layout15 = QHBoxLayout(None,0,6,"layout15")

        layout14 = QVBoxLayout(None,0,6,"layout14")

        #self.writeRButton = QRadioButton(self.buttonGroup2,"writeRButton")
        #self.writeRButton.setChecked(1)
        #layout14.addWidget(self.writeRButton)

        #self.askRButton = QRadioButton(self.buttonGroup2,"askRButton")
        #layout14.addWidget(self.askRButton)
        #layout15.addLayout(layout14)

        self.sendButton = QPushButton(self.buttonGroup2,"sendButton")
        layout15.addWidget(self.sendButton)
        layout16.addLayout(layout15)
        layout18.addLayout(layout16)

        layout17 = QHBoxLayout(None,0,6,"layout17")

        self.textLabel3 = QLabel(self.buttonGroup2,"textLabel3")
        layout17.addWidget(self.textLabel3)

        self.answerLine = QLineEdit(self.buttonGroup2,"answerLine")
        layout17.addWidget(self.answerLine)
        layout18.addLayout(layout17)

        layout9 = QHBoxLayout(None,0,6,"layout9")
        spacer3 = QSpacerItem(381,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
        layout9.addItem(spacer3)

        self.closeButton = QPushButton(self.buttonGroup2,"closeButton")
        layout9.addWidget(self.closeButton)
        layout18.addLayout(layout9)

        buttonGroup2Layout.addLayout(layout18,0,0)

        visatesterLayout.addWidget(self.buttonGroup2,0,0)

        self.languageChange()

        #self.resize(QSize(600,223).expandedTo(self.minimumSizeHint()))
        #self.clearWState(Qt.WState_Polished)

        self.connect(self.closeButton,SIGNAL("clicked()"),self.close)
	self.connect(self.sendButton,SIGNAL("clicked()"),self.send)


    def languageChange(self):
        self.setCaption(self.__tr("VisaTester"))
        self.buttonGroup2.setTitle(self.__tr("VisaTester..."))
        self.textLabel1.setText(self.__tr("Send to instrument with VISA-Adress:"))
        self.textLabel2.setText(self.__tr("the VISA/GPIB command:"))
        self.commandCombo.clear()
        self.commandCombo.insertItem(self.__tr("*IDN?"))
        self.commandCombo.insertItem(self.__tr("*rst"))
        self.commandCombo.insertItem(self.__tr("INIT"))
        self.commandCombo.insertItem(self.__tr("read?"))
        self.commandCombo.insertItem(self.__tr("fetch?"))
        self.commandCombo.insertItem(self.__tr("*TRG"))
        #self.writeRButton.setText(self.__tr("just send command"))
        #self.askRButton.setText(self.__tr("send command and read answer"))
        self.sendButton.setText(self.__tr("Send"))
        self.textLabel3.setText(self.__tr("Answer:"))
        self.closeButton.setText(self.__tr("close"))
	QToolTip.add( self.sendButton, "Send string above, if it contains a question mark (?), it will read also the answer")
	QToolTip.add( self.commandCombo, "Enter command you want to send to the instrument")
	QToolTip.add( self.gpibCombo, "Enter GPIB/VISA address of the instrument, you want to talk with")
	QToolTip.add( self.answerLine, "(Last) answer of the instrument")

    def send(self):
        print "Sending..."
	instr=Instrument(str(self.gpibCombo.currentText()))
	instr.timeout=10
	command=str(self.commandCombo.currentText())
	self.answerLine.setText("")
	if command.find("?")==-1:
		instr.write(command)
	else:
		answ=instr.ask(command)
		self.answerLine.setText(str(answ))

		

    def __tr(self,s,c = None):
        return qApp.translate("visatester",s,c)
